package main

import (
	"encoding/json"
	"fmt"
)

type struct_Monster struct {
	Name string
	Age int
	Birthday string
	Sal float64
	Skill string
}

//对struct序列化
func testMonster(){
	monster := struct_Monster{
		Name:"monster",
		Age:20,
		Birthday:"2020-01-01",
		Sal:34.5,
		Skill:"大锤",
	}

	data, err := json.Marshal(&monster)
	if err != nil{
		fmt.Println("序列化失败", err)
		return
	}
	fmt.Println(string(data))
}

//对map序列化
func testMap(){
	var a map[string]interface{}

	a = make(map[string]interface{})
	a["name"] = "红孩儿"
	a["age"] = 30
	a["address"] = "天上"

	data, err := json.Marshal(a)
	if err != nil{
		fmt.Println("序列化失败", err)
		return
	}
	fmt.Println(string(data))
}

//对切片序列化
func testSlice(){
	var slice []map[string]interface{}
	var m1 map[string]interface{}
	m1 = make(map[string]interface{})
	m1["name"] = "红孩儿er"
	m1["age"] = 32
	m1["address"] = "天上a"
	slice = append(slice, m1)

	var m2 map[string]interface{}
	m2 = make(map[string]interface{})
	m2["name"] = "红孩儿er"
	m2["age"] = 32
	m2["address"] = [2]string{"天上a", "dixia"}
	slice = append(slice, m2)

	data, err := json.Marshal(slice)
	if err != nil{
		fmt.Println("序列化失败", err)
		return
	}
	fmt.Println(string(data))
}

//对基本数据类型序列化并没有什么特别的意义
func testNormal(){
	var num1 float64 = 222.44
	data, err := json.Marshal(num1)
	if err != nil{
		fmt.Println("序列化失败", err)
		return
	}
	fmt.Println(string(data))
}

func main() {
	testMonster()
	testMap()
	testSlice()
	testNormal()
}